package com.wang.ddp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wang.ddp.dto.Result;
import com.wang.ddp.dto.UserDTO;
import com.wang.ddp.entity.Follow;
import com.wang.ddp.entity.User;
import com.wang.ddp.mapper.FollowMapper;
import com.wang.ddp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.ddp.utils.UserHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Resource
    UserServiceImpl userService;
    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        //1、获取登录用户id
        Long userId = UserHolder.getUser().getId();
        String key = "follows:"+userId;
        //1.1、判断是关注还是取关
        if(isFollow){
            //2、关注：新增数据
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(followUserId);
            boolean isSuccess = this.save(follow);
            if (isFollow){
                //把关注目标用户id放进redis的set集合 sadd follows:userId followerUserId
                stringRedisTemplate.opsForSet().add(key, followUserId.toString());
            }
        } else {
            //3、取关：删除
            LambdaQueryWrapper<Follow> followLambdaQueryWrapper = new LambdaQueryWrapper<>();
            followLambdaQueryWrapper.eq(Follow::getFollowUserId,followUserId).eq(Follow::getUserId,userId);
            boolean remove = this.remove(followLambdaQueryWrapper);
            if (remove){
                //把关注目标用户id从redis的set集合移除 sadd follows:userId followerUserId
                stringRedisTemplate.opsForSet().remove(key, followUserId.toString());
            }

        }
        return Result.ok();
    }

    @Override
    public Result queryFollow(Long followUserId) {
        Long userId = UserHolder.getUser().getId();
        LambdaQueryWrapper<Follow> followLambdaQueryWrapper = new LambdaQueryWrapper<>();
        followLambdaQueryWrapper.eq(Follow::getFollowUserId,followUserId).eq(Follow::getUserId,userId);
        int count = this.count(followLambdaQueryWrapper);
        return Result.ok(count > 0);
    }

    /**
     * 查找当前用户和目标用户的共同关注
     * @param id
     * @return
     */
    @Override
    public Result followCommons(Long id) {
        UserDTO user = UserHolder.getUser();
        if (user == null){
            return Result.ok();
        }
        Long userId = user.getId();
        String key1 = "follows:" + userId;
        String key2 = "follows:" + id;
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key1, key2);
        //解析id集合
        if (intersect == null || intersect.isEmpty()){
            return Result.ok(Collections.emptyList());
        }
        List<Long> userIds = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        List<User> userList = userService.listByIds(userIds);
        //转成DTO
        List<UserDTO> userDTOList = userList.stream().map((item) -> {
            return BeanUtil.copyProperties(item, UserDTO.class);
        }).collect(Collectors.toList());
        return Result.ok(userDTOList);
    }
}
